var is_mobile=false;

var slides_per_view=2;

if ($(window).outerWidth()<959){
    is_mobile=true;
}

if ($(window).outerWidth()>1900){
    slides_per_view=3;
}
if ($(window).outerWidth()>2100){
    slides_per_view=4;
}
if ($(window).outerWidth()>2100){
    slides_per_view=5;
}

$().ready(function (e) {
    startMainScroll();
    setTimeout(function(){
        $(".main_cover").addClass("hide");
    },1500)

    setTimeout(function(){
        $(".menu_loading_bar").addClass("show");
    },500)

    setTimeout(function(){
        $(".menu_loading_container").addClass("hide");

    },1500)

    huiyuanAmInit();
});

function huiyuanAmInit(){
    $(".huiyuan_am .open_icon").bind("click",function(){

        $(".huiyuan_am").removeClass("active");
        $(this).parent().parent().parent().addClass("active");
    })

    $(".huiyuan_am .close_icon").bind("click",function(){
        $(".huiyuan_am").removeClass("active");
    })
}

function swiperNavInit(container,num,swiper){
    while($(container).find(".swiper_nav_item").numChildren>1){
        $(container).find(".swiper_nav_item").removeChildAt(1);
    }

    for (var i=1;i<=num;i++){
        var item;
        if (i==1){
            item=$(container).find(".swiper_nav_item")[0];
        }
        else{
            item=$($(container).find(".swiper_nav_item")[0]).clone();
            $(container).find(".swiper_nav").append(item);
        }
    }

    changeSwiperNavActive(container,0,swiper);


    setInterval(function(){
        var swiper_progress=swiper.params.progress;
        $(container).find(".swiper_nav_active i").css("transform","scale("+swiper_progress+",1)");
    },40);
}

function changeSwiperNavActive(container,num,swiper){

    $(container).find(".swiper_nav_item").removeClass("swiper_nav_active");
    $($(container).find(".swiper_nav_item")[num]).addClass("swiper_nav_active");

    $(container).find(".swiper_nav_active i").css("transform","scale(1,1)");

    $(container).find("i").each(function(index,ele){
        ele.style="";
    });
    swiper.params.progress=0;
    TweenMax.to(swiper.params,5,{overwrite:true,progress:1,ease:Linear.easeNone});
    //}});
}

function changeImgSwiperTxt(num){
    num=num==-1? $(".img_txt_container li").length-1:num;
    $(".img_txt_container li").removeClass("active");
    $($(".img_txt_container li")[num]).addClass("active");
}


function getMatrix(element) {
    const values = element.style.transform.split(/\w+\(|\);?/);
    const transform = values[1].split(/,\s?/g).map(parseInt);

    return {
        x: transform[0],
        y: transform[1],
        z: transform[2]
    };
}


var total_grid_num=2
var current_grid_num=1;
var change_grid_st=0;
var grid_swiper={};
function changeGridInit(){


    grid_swiper.params={};
    grid_swiper.params.progress=0;

    swiperNavInit($('.fang_container'),total_grid_num,grid_swiper);

    startChangeGrid();
}

function startChangeGrid(){
    changeSwiperNavActive(".fang_container",current_grid_num-1,grid_swiper);
    $(".main_grid").removeClass("grid_active");
    $($(".main_grid")[current_grid_num-1]).addClass("grid_active");

    current_grid_num++;
    current_grid_num=current_grid_num>total_grid_num?1:current_grid_num;
    change_grid_st=setTimeout(startChangeGrid,5000);


}



//moveImgInit();
var move_img_y_si=0;
function startMoveImgY(){
    move_img_y_si=setInterval(function(){
        moveImgYRender();
    },40)
}

function stopMoveImgY(){
    clearInterval(move_img_y_si);
}

function moveImgYRender(){
    var sh=$(window).outerHeight();
    var window_scroll_y=window.scrollY

    $(".move_img_y").each(function(index,ele){
        var scale=1;
        //TweenMax.set(ele,{scale:scale,force3D:true});

        var img_height=$(ele).height();
        var parent_height=$(ele).parent().height();
        /*var offset_y=$(ele).parent().offset().top-(window_scroll_y-parent_height);
        var top_y=((img_height*scale-parent_height)/2);
        var check_height=sh+parent_height;
        var move_y=top_y-(((offset_y))/check_height)*((img_height*scale-parent_height));*/
        var offset_y=window_scroll_y+sh-$(ele).parent().offset().top;
        var top_y=(img_height-parent_height)/2;
        var check_height=sh+parent_height;
        var move_h=2*((img_height-parent_height)/2);
        var move_y=-top_y+(((offset_y))/check_height)*((move_h));

        TweenMax.set(ele,{y:move_y,force3D:true});

        $(ele).parent().css("overflow","hidden")
    })
}

function moveImgCenter(){
    $(".move_img_y").each(function(index,ele){
        var img_width=$(ele).width();
        var img_height=$(ele).height();
        var parent_width=$(ele).parent().width();
        var parent_height=$(ele).parent().height();


        $(ele).css("margin-left",(parent_width-img_width)/2);
        $(ele).css("margin-top",(parent_height-img_height)/2);
    })

    $(".move_img_x").each(function(index,ele){
        var img_width=$(ele).width();
        var img_height=$(ele).height();
        var parent_width=$(ele).parent().width();
        var parent_height=$(ele).parent().height();


        $(ele).css("margin-left",(parent_width-img_width)/2);
        $(ele).css("margin-top",(parent_height-img_height)/2);
    })
}

var move_img_x_si=0;
function startMoveImgX(){
    move_img_x_si=setInterval(function(){
        moveImgXRender();
    },40)
}

function stopMoveImgX(){
    clearInterval(move_img_x_si);
}

function moveImgXRender(){
    var sw=$(window).outerWidth();

    $(".move_img_x").each(function(index,ele){
        var scale=1;
        //TweenMax.set(ele,{scale:scale,force3D:true});
        var img_width=$(ele).width();
        var parent_width=$(ele).parent().width();

        var offset_x=$(ele).parent().offset().left+parent_width;
        var left_x=-(img_width-parent_width)/2;
        var check_width=parent_width+sw;
        var move_x=left_x+(img_width-parent_width)*((offset_x)/(check_width));

        TweenMax.set(ele,{x:move_x,force3D:true});


    })


}


var main_scroll_si=0;

function startMainScroll(){
    main_scroll_si=setInterval(function () {
        checkMainScrollRender();
    }, 100)
}


function checkMainScrollRender(){
    var window_scroll_y=window.scrollY
    var body_height = $(window).outerHeight();
    var delay_show_distance=200;
    $(".check_scroll_container").each(function (index,ele){
        if ($(ele).attr("in_screen")!=1) {
            var offset_top = $(ele).offset().top;
            if (window_scroll_y + body_height - delay_show_distance > offset_top) {
                $(ele).attr("in_screen", 1);

                normalTitleAnimate($(ele).find(".title_pre_am"));

                $(ele).find(".visible_pre_am").addClass("normal_visible_am")

                $(ele).find(".round_img_pre_am").addClass("round_img_am")
            }
        }
    })
}

function normalTitleAnimate(container){
    var ran_time=500;
    $(container).find("span").each(function(index,ele){
        setTimeout(function (){
            $(ele).addClass("normal_visible_am");
        },Math.random()*ran_time);
    })
}

var head_screen_move_si=0;
var head_screen_move_enabled=true;
function headScreenMoveStart(){
    head_screen_move_si=setInterval(function(){

        headScreenMoveRender();
    },20)
}
function headScreenMoveRender(){


    if (is_mobile){
        //var move_y=($(window).outerHeight()-$(".main").offset().top)/2;
        //var move_y=-screen_swiper.translate/2;
        /*if (move_y<0||move_y/$(window).outerHeight()>1){
            return;
        }*/
        //console.log(screen_swiper.translate);
        //TweenMax.set(".head_screen_move",{y:move_y,force3D:true});
    }
    else {
        var move_y=window.scrollY/5;
        //console.log(move_y);
        TweenMax.set(".head_screen_move",{y:move_y,force3D:true});
    }
}

var screen_swiper;
var screen2_swiper;
function mobileHeadSwiper(){
    screen_swiper = new Swiper('#top_container', {
        direction: 'vertical',
        speed:800,
        on: {
            transitionStart: function () {
                //head_screen_move_enabled = false;
                $(".head_screen_move").addClass("head_screen_move_ease");

                if (screen_swiper.realIndex == 0) {
                    TweenMax.set(".head_screen_move", {y: 0, force3D: false});
                }
                if (screen_swiper.realIndex == 1) {
                    TweenMax.set(".head_screen_move", {y: $(window).outerHeight() / 2, force3D: false});

                }
                //TweenMax.to($(".head .content"),3,{opacity:1});

            },
            transitionEnd: function () {
                //head_screen_move_enabled = false;

                //var head_content=$(".head .content").clone();
                //$(".head .content").remove();
                //$(".head").append(head_content);
            }
        }

    });
    screen2_swiper = new Swiper('#main',{//子swiper
        direction: 'vertical',
        nested:true,
        //resistanceRatio: 0,
        slidesPerView: 'auto',
        freeMode: true,
        momentumBounce:false

    })



}

function mobileHeadInit(){
    var sh=$(window).outerHeight();
    TweenMax.set($("#main"),{y:sh,force3D: false});
}

var mobile_scroll_si=0;

function startMobileScroll(){
    mobile_scroll_si=setInterval(function () {
        checkMobileScrollRender();
    }, 40)
}

var mobile_scroll_y1=0;
var mobile_scroll_y2=0;

function checkMobileScrollRender(){
    var window_scroll_y=window.scrollY
    var body_height = $(window).outerHeight();
    var delay_show_distance=300;

    mobile_scroll_y2=window.scrollY;

    if (window_scroll_y<body_height){
        TweenMax.set($("#main"),{y:body_height-window_scroll_y/4,force3D:false});
    }
}